package com.liang.hellocompose

import android.text.format.DateUtils
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Colors
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.liang.hellocompose.data.HomeItemEntity
import com.liang.hellocompose.util.DateUtil


/**
 * @author suzl
 * @createTime 2021/8/3
 * @Description
 */

@Composable
fun HomePageItem(data: HomeItemEntity, clickListener: () -> Unit = {}) {

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .clickable {
                clickListener()
            }
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .padding(12.dp, 12.dp, 12.dp)
        ) {
            if (data.fresh) {
                TagText(text = "新", color = Color(237, 102, 97))
                Spacer(modifier = Modifier.width(10.dp))
            }
            if (!data.tags.isNullOrEmpty()) {
                data.tags.forEach {
                    TagText(text = it.name, color = Color(74, 214, 225))
                    Spacer(modifier = Modifier.width(10.dp))
                }
            }
            Text(
                modifier = Modifier
                    .weight(1f)
                    .align(Alignment.CenterVertically),
                text = if (data.author.isNullOrBlank()) data.shareUser else data.author,
                style = TextStyle(
                    color = Color.Gray,
                    fontSize = 12.sp
                )
            )
            Text(
                modifier = Modifier.align(Alignment.CenterVertically),
                text = getDateStr(data.shareDate),
                style = TextStyle(
                    color = Color.Gray,
                    fontSize = 12.sp
                )
            )
        }
        Text(
            modifier = Modifier
                .fillMaxWidth()
                .padding(12.dp, 10.dp)
                .align(Alignment.Start),
            maxLines = 2,
            overflow = TextOverflow.Ellipsis,
            text = data.title,
            style = TextStyle(
                color = Color.Black,
                fontSize = 15.sp
            )
        )
        if (!data.desc.isNullOrBlank())
            Text(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(12.dp, 0.dp, 12.dp, 10.dp)
                    .align(Alignment.Start),
                text = data.desc,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
                style = TextStyle(
                    color = Color.Gray,
                    fontSize = 12.sp
                )
            )
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(12.dp, 0.dp, 12.dp, 12.dp)
        ) {
            Text(
                modifier = Modifier
                    .weight(1f)
                    .align(Alignment.Bottom),
                text = "${data.superChapterName} / ${data.chapterName}",
                style = TextStyle(
                    color = Color.Gray,
                    fontSize = 12.sp
                )
            )
            Image(
                modifier = Modifier
                    .width(20.dp)
                    .height(20.dp),
                painter = painterResource(id = R.mipmap.ic_followed),
                contentDescription = null,
                colorFilter = ColorFilter.tint(
                    Color.LightGray,
                    blendMode = if (data.collect) BlendMode.Dst else BlendMode.SrcAtop
                )
            )
        }
    }
}

private fun getDateStr(timestamp: Long?): String {

    return when {
        timestamp == null || timestamp <= 0 -> ""
        //今天
        DateUtils.isToday(timestamp) -> DateUtil.format(timestamp, "HH:mm")
        //昨天
        DateUtils.isToday(timestamp + DateUtils.DAY_IN_MILLIS) ->
            "昨天 ${DateUtil.format(timestamp, "HH:mm")}"
        //前天
        DateUtils.isToday(timestamp + 2 * DateUtils.DAY_IN_MILLIS) ->
            "前天 ${DateUtil.format(timestamp, "HH:mm")}"
        //今年
        DateUtil.isYear(timestamp) -> DateUtil.format(timestamp, "MM-dd HH:mm")
        //其他
        else -> DateUtil.format(timestamp, "yyyy-MM-dd HH:mm")
    }
}

@Composable
private fun TagText(text: String, color: Color) {
    Box(
        modifier = Modifier
            .border(
                width = 1.dp,
                color = color,
                shape = RoundedCornerShape(5.dp)
            )
    ) {
        Text(
            modifier = Modifier.padding(4.dp, 2.dp),
            text = text,
            style = TextStyle(
                color = color,
                fontSize = 12.sp
            )
        )
    }
}

@Preview
@Composable
fun previewHomeItem() {
    HomePageItem(data = HomeItemEntity())
}
